package com.zy.socket.practies;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Author Gong
 * @Description 服务端读取图片并发送给客户端，客户端保存图片到本地
 * @dATE 2021/12/19 10:41
 **/
public class Server1 {
    public static void main(String[] args) {
        InputStream inputStream = null;
        Socket accept =null;
        ServerSocket serverSocket =null;
        try {
             serverSocket = new ServerSocket(8989);
             accept = serverSocket.accept();
            System.out.println(accept);
            OutputStream outputStream = accept.getOutputStream();
            FileInputStream fileInputStream = new FileInputStream(new File("d:/temp/java.png"));
            byte[] by= new byte[1024];
            int len;
            while ((len=fileInputStream.read(by)) > -1){
                outputStream.write(by,0,len);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(accept!=null){
                try {
                    accept.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(serverSocket!=null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}
